// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	2/21/2015 11:06:23 PM				
// -----------------------------

#include "Log.h"

#include <stdio.h>
#include <stdarg.h>

#include "PlatformProvider.h"
#include "Common.h"
#include "Engine.h"
#include "IPlatform.h"
#include "IMutex.h"

namespace Flow
{

	IMutex* Log::s_lpWriteLock = nullptr;

	void Log::Initialize()
	{
		s_lpWriteLock = PlatformProvider::QueryInterface<IMutex>("Mutex");
		ASSERT(s_lpWriteLock);
	}
	void Log::Uninitialize()
	{
		s_lpWriteLock->Release();
	}

	void Log::Write(const char* format, ...)
	{
		const Size_T maxLen = 512;
		char line[maxLen] = {};

		va_list args;
		va_start(args, format);

		vsprintf_s(line, maxLen, format, args);

		va_end(args);

		OutputDebugStringA(line);
	}

	void Log::WriteLine(const char* format, ...)
	{
		const Size_T maxLen = 512;
		char line[maxLen] = {};

		va_list args;
		va_start(args, format);
		vsprintf_s(line, maxLen, format, args);
		va_end(args);

		UInt32 threadId = (Engine::Platform) ? (Engine::Platform->GetCurrentThreadID()) : (0);
		s_lpWriteLock->Lock();
		Write("[LOG]\t (Thread: 0x%05X)\t\t", threadId);
		Write(line);
		Write("\n");
		s_lpWriteLock->Unlock();
	}

}